package com.yz.lzq.liwushuo.ui.modules.gift.top100;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.decoration.SpacesItemDecoration;
import com.yz.lzq.liwushuo.jsonbean.gift.GiftgoryTop100Bean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by AdminJia on 2016/11/25.
 */
public class GiftTopFragment extends Fragment implements IgiftTop100View {
    @Bind(R.id.xRecyclerView_gift_top100)
    XRecyclerView xRecyclerViewGiftTop100;
    private Context mContext = null;
    private View header;
    private GiftgoryTop100Presenter presenter;
    private SimpleDraweeView simpleDraweeView;
    private int offset = 0;
    private boolean flag = false;
    private boolean flag1 = false;
    private GiftTop100Adapter adapter = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        presenter = new GiftgoryTop100Presenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gift_top, container, false);
        ButterKnife.bind(this, view);
        return view;
    }
//
//    {
//        mRecyclerView = (XRecyclerView) this.findViewById(R.id.recyclerview);
//        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
//        mRecyclerView.setLayoutManager(layoutManager);
//
//        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
//        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
//        mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
//
//        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
//            @Override
//            public void onRefresh() {
//                refreshTime++;
//                times = 0;
//                new Handler().postDelayed(new Runnable() {
//                    public void run() {
//
//                        listData.clear();
//                        for (int i = 0; i < 20; i++) {
//                            listData.add("item" + i + "after " + refreshTime + " times of
// refresh");
//                        }
//                        mAdapter.notifyDataSetChanged();
//                        mRecyclerView.refreshComplete();
//                    }
//
//                }, 1000);            //refresh data here
//            }
//
//            @Override
//            public void onLoadMore() {
//                if (times < 2) {
//                    new Handler().postDelayed(new Runnable() {
//                        public void run() {
//                            mRecyclerView.loadMoreComplete();
//                            for (int i = 0; i < 20; i++) {
//                                listData.add("item" + (i + listData.size()));
//                            }
//                            mRecyclerView.loadMoreComplete();
//                            mAdapter.notifyDataSetChanged();
//                        }
//                    }, 1000);
//                } else {
//                    new Handler().postDelayed(new Runnable() {
//                        public void run() {
//                            for (int i = 0; i < 9; i++) {
//                                listData.add("item" + (i + listData.size()));
//                            }
//                            mAdapter.notifyDataSetChanged();
//                            mRecyclerView.setNoMore(true);
//                        }
//                    }, 1000);
//                }
//                times++;
//            }
//        });
//
//        listData = new ArrayList<String>();
//        for (int i = 0; i < 20; i++) {
//            listData.add("item" + i);
//        }
//        mAdapter = new MyAdapter(listData);
//
//        mRecyclerView.setAdapter(mAdapter);
//
//    }

//}

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        GridLayoutManager manager = new GridLayoutManager(mContext, 2);

        xRecyclerViewGiftTop100.addItemDecoration(new SpacesItemDecoration(5));

        xRecyclerViewGiftTop100.setLayoutManager(manager);
        xRecyclerViewGiftTop100.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerViewGiftTop100.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerViewGiftTop100.setArrowImageView(R.mipmap.iconfont_downgrey);
        header = LayoutInflater.from(mContext).inflate(R.layout.item_gift_vp_top100_header, null);
        simpleDraweeView = (SimpleDraweeView) header.findViewById(R.id
                .simpleDraweeView_gift_recyclerView_header);

        presenter.getData(mContext, offset);

        xRecyclerViewGiftTop100.addHeaderView(header);
        xRecyclerViewGiftTop100.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (!flag1) {
                    // Toast.makeText(mContext, "刷新了", Toast.LENGTH_SHORT).show();
                    flag1 = !flag1;
                    offset = 0;
                    presenter.getMoreData(offset);
                    xRecyclerViewGiftTop100.refreshComplete();
                    flag1 = !flag1;

                } else {
                    xRecyclerViewGiftTop100.refreshComplete();
                    Toast.makeText(mContext, "滑慢点， 慢点。。。。。", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onLoadMore() {
                if (!flag && adapter.getItemCount() > offset) {
                    flag = !flag;
                    offset += 20;
                    presenter.getMoreData(offset);
                    xRecyclerViewGiftTop100.loadMoreComplete();
                    // Toast.makeText(mContext, "jiazaile ", Toast.LENGTH_SHORT).show();
                    Log.i("TAG", "-----------onLoadMore: " + flag + offset);
                    flag = !flag;
                } else if (flag) {
                    xRecyclerViewGiftTop100.loadMoreComplete();
                    Toast.makeText(mContext, "滑慢点， 慢点。。。。。", Toast.LENGTH_SHORT).show();
                    Toast.makeText(mContext, "jiazaile ", Toast.LENGTH_SHORT).show();
                }

                xRecyclerViewGiftTop100.loadMoreComplete();


            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void setAdapterSuccess(GiftTop100Adapter _adapter) {
        adapter = _adapter;
        xRecyclerViewGiftTop100.setAdapter(adapter);


    }


    @Override
    public void setImgUrl(String imgUrl) {
        simpleDraweeView.setImageURI(Uri.parse(imgUrl));
    }

    @Override
    public void addItems(List<GiftgoryTop100Bean.DataBean.ItemsBean> _items) {
        adapter.additems(_items);

    }

    @Override
    public void faled(Throwable t) {

    }
}
